﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YTSK.Common
{
    class handleServer
    {
        public static string _token;
        public static string _serveraddress;


        #region 获取登录的token--覃豪@20170225
        /// <summary>
        /// 获取登录的token
        /// </summary>
        /// <param name="cAddress">服务器地址</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public static void Login(string addr, string username, string password)
        {
            _serveraddress = addr;
            #region 获取登录的token
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("UserName", username);// "printControl"
            dic.Add("Password", password);// "cSysAABBCCaaa"       
            var url = _serveraddress + "/api/SysCommon/Login";
            string s = handleHttp.Post(dic, url);
            var d1= Common.handleJSON.Deserialize(s);
            var suc = d1.getValue("result").ObjToInt();
            if (suc == -1)
            {
                var err = d1.getValue("resultdetail").ObjToString();
                throw new Exception("出现错误" + Environment.NewLine + err);
            }
            _token = d1.getValue("token").ObjToString();
            #endregion
        }
        #endregion

        #region 从服务器获取数据
        public static string GetData(string addr, Dictionary<string, object> dicUrl, bool addToken = true)
        {
            if (addToken) { dicUrl.Add("token", _token); }
            #region 获取登录的token 
            var url = _serveraddress + addr + "?" + dicUrl.ToUrl();
            string s = handleHttp.Get(url);
            return s;
            #endregion
        }

        #endregion
    }
}
